package VistaGraficos;

import java.awt.*;
import java.util.Vector;

import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.text.TextBlockAnchor;
import org.jfree.ui.*;
import org.jfree.util.Log;
import org.jfree.util.PrintStreamLogTarget;

public class GraficoBarra extends JFrame {

	private Vector mess1=null;
	private Vector mess2=null;
	private String nameMes;
	private String nameMes2;
  public GraficoBarra() {
    super("Ventas Mensuales");

    getContentPane().add(createDemoPanel());
    pack();
    //setVisible(true);
 
  }
  public void setDatos_Barras(Vector mes1,Vector mes2,String d,String h){
	  
	  this.mess1=mes1;
	  this.mess2=mes2;
	  this.nameMes=d;
	  this.nameMes2=h;
	  
	  setVisible(true);
  }


  /**
   * Devuelve los datos del grafico
   */
  private CategoryDataset createDataset() {
    DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();

    defaultcategorydataset.addValue(this.mess1.size(),this.nameMes, "");
    defaultcategorydataset.addValue(this.mess2.size(),this.nameMes2, "");
    
    
    return defaultcategorydataset;
  }

  /**
   * Devuelve el grafico construido en funcion de los datos que le son pasados.
   */
  private JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createBarChart3D("Ventas del Mes","Mes","Cantidad", categorydataset, PlotOrientation.VERTICAL, true, true, false);
    
    CategoryPlot categoryplot = jfreechart.getCategoryPlot();
           
    categoryplot.setAnchorValue(2D);
    
    categoryplot.setForegroundAlpha(1.0F);
    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
    
            
    CategoryLabelPositions categorylabelpositions = categoryaxis.getCategoryLabelPositions();
    CategoryLabelPosition categorylabelposition = new CategoryLabelPosition(RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, 0.0D, CategoryLabelWidthType.RANGE, 0.5F);
    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.replaceRightPosition(categorylabelpositions, categorylabelposition));
    
    return jfreechart;
  }

  /**
   * Devuelve un JPanel con el grafico
   */
  public JPanel createDemoPanel() {
    JFreeChart jfreechart = createChart(createDataset());
    return new ChartPanel(jfreechart);
  }

  public static void main(String[] args) {
    GraficoBarra barra = new GraficoBarra();
  }

}